package com.cpi.cornpp.purchase.program.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseOrderAssignFutureForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderAssignFuturesView;

public class PurchaseProgramAssignSaveAction extends Action {
	/** Success forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException {
		HttpSession httpSession = null;
		PurchaseProgramService purchaseProgramService = null;
		PurchaseOrderAssignFutureForm purchaseOrderAssignFutureForm = null;
		PurchaseOrderAssignFuturesView purchaseOrderAssignFuturesView = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			purchaseProgramService = new PurchaseProgramService();
			purchaseOrderAssignFutureForm = (PurchaseOrderAssignFutureForm)form;
			purchaseOrderAssignFuturesView = purchaseOrderAssignFutureForm.getPurchaseOrderAssignFuturesView();
			purchaseProgramService.savePuchaseOrderAssignFuture(purchaseOrderAssignFuturesView);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
			httpSession.removeAttribute("previousOrderField");
			httpSession.removeAttribute("previousOrderType");
		}catch (PurchaseProgramException purchaseProgramException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		}
		return mapping.findForward(DEFAULT_FORWARD);	
	}
}